#include <mylib/device/led.h>
#include <mylib/driver/gpio.h>

void led_init(led_t* led, uint32_t gpio)
{
    led->gpio = gpio;    
}

// 开灯
void led_on(led_t* led)
{
    if (led->valid == VALID_ON_HIGH_LEVEL) {
        gpio_set_value(led->gpio, GPIO_LEVEL_HIGH);
    }
    else {
        gpio_set_value(led->gpio, GPIO_LEVEL_LOW);
    }
    led->state = led_state_on;
}

// 关灯
void led_off(led_t* led)
{
    if (led->valid == VALID_ON_HIGH_LEVEL) {
        gpio_set_value(led->gpio, GPIO_LEVEL_LOW);
    }
    else {
        gpio_set_value(led->gpio, GPIO_LEVEL_HIGH);
    }
    led->state = led_state_off;
}

// 切换灯的状态
void led_toggle(led_t* led)
{
    if (led->state == led_state_on) {
        led_off(led);
    }
    else {
        led_on(led);
    }
}

